package com.gfm.story.controller;


import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.DictionaryDetail;
import com.gfm.story.service.dict.DictionaryDetailService;
import com.gfm.story.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Description: 数据字典子集控制层
 * @Author: ly
 * @CreateDate: 2019/3/7 0007 下午 5:01
 * @UpdateUser: yc
 * @UpdateDate: 2019/3/7 0007 下午 5:01
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(tags = "系统管理-数据字典子集接口")
@RestController
@RequestMapping(value = "/dictDetail", method = RequestMethod.POST)
public class DictionaryDetailController {

    @Resource
    private DictionaryDetailService sysDictionaryDetailService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    /**
     * 获取所有数据字典子集列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSysDictionaryDetailList.do",produces="application/json;charset=UTF-8")
    @ApiOperation(value = "获取所有数据字典子集列表", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictDatailName", value = "名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "dictDatailCode", value = "编码", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "dictId", value = "父级ID", dataType = "String", paramType = "query", required = true)
    })
    public ResultBean getSysDictionaryDetailList(HttpServletRequest request) throws Exception {
        request.setCharacterEncoding("UTF-8");
        Map<String, Object> map = ParamUtil.getParameterMap(request);
        String dictDatailName = request.getParameter("dictDatailName");
        if (null != dictDatailName) {
            map.put("dictDatailName", dictDatailName);
        }
        ResultBean resultBean = this.sysDictionaryDetailService.selectSysDictionaryDetailList(map);
        return resultBean;
    }

    /**
     * 新增或者修改数据字典子集信息
     *
     * @param sysDictionaryDetail
     * @return
     */
    @RequestMapping("/addOrEdit.do")
    @ApiOperation(value = "新增或者修改数据字典子集信息", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictDatailName", value = "名称", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "dictDatailCode", value = "编码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "dictId", value = "父级ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "dictDatailDisc", value = "描述", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "dictOrder", value = "排序", dataType = "String", paramType = "query", required = false)

    })
    public ResultBean addOrEdit(@Validated DictionaryDetail sysDictionaryDetail) {
        return this.sysDictionaryDetailService.addOrEdit(sysDictionaryDetail);
    }

    /**
     * 删除数据字典子集,支持批量删除
     *
     * @param
     * @return
     */

    @RequestMapping("/remove.do")
    @ApiOperation(value = "删除数据字典信息", httpMethod = "POST", response = ResultBean.class)

    public ResultBean remove(Long id) {
        return this.sysDictionaryDetailService.remove(id);
    }

    /**
     * 根据数据字典编码获取所有数据字典子集列表
     *
     * @param dictCode
     * @return
     * @throws Exception
     */
    @RequestMapping("/getSysDictionaryDetailListByDictCode.do")
    @ApiOperation(value = "根据数据字典编码获取所有数据字典子集列表(公共方法)", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictCode", value = "编码", dataType = "String", paramType = "query", required = true)
    })
    public ResultBean SysDictionaryDetailListByDictCode(String dictCode,String paltCode) {

        return this.sysDictionaryDetailService.SysDictionaryDetailListByDictCode(dictCode,paltCode);
    }



}
